package all.com.jinyongqunxiax.model.mod;

import com.randy.fundation.JxAssert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import jy.game.components.item.JyItem;
import jy.game.tools.BasePojoManager;

public class ItemManager {
    private static final String              NODE_NAME = "item";
    public               Map<String, JyItem> map       = new HashMap<>();
    public               List<JyItem>        list      = new ArrayList<>();
    String xmlPath;

    public ItemManager(@NonNull String xmlPath) {
        this.xmlPath = xmlPath;
    }

    public BasePojoManager<JyItem> manager;

    public String load() {
        map.clear();
        list.clear();

        manager = BasePojoManager.build(xmlPath, JyItem.class);
        list = manager.datas;
        if (!manager.success()) {
            return manager.message;
        }
        StringBuilder message = new StringBuilder(getClass().getSimpleName());

        for (JyItem item : list) {
            if (map.containsKey(item.name)) {
                message.append("发现重复:").append(item.name);
                continue;
            }
            map.put(item.name, item);
        }

        if (all.com.jinyongqunxiax.BuildConfig.DEBUG) {
            JxAssert.sure(map.entrySet().size() == list.size());
        }
        return manager.message;
    }

//    @wx.xml.simpleframework.xml.Root(name = "root")
//    static public class Root {
//        @ElementList(inline = true) List<Model> models;
//    }
//
//    @wx.xml.simpleframework.xml.Root(name = "item")
//    static public class Model {
//        @Attribute(required = false) public int     cd;
//        public @Attribute                   String  name;
//        public @Attribute                   String  desc;
//        public @Attribute                   boolean drop;
//        @Attribute(name = "pic") public     String  icon;
//        public @Attribute                   int     price;
//        public @Attribute                   String  talent;
//        public @Attribute                   int     type;
//        public @Attribute                   int     level;
//
//        public @ElementList(inline = true, required = false) List<Trigger> triggers;
//        public @ElementList(inline = true, required = false) List<JyRequire> requires;
//    }
//
//    static public class Trigger {
//        public @Attribute                 String name;
//        public @Attribute(name = "argvs") String value;
//
//        @NonNull
//        @Override
//        public String toString() {
//            return Mod.triggerName(name) + ":" + value;
//        }
//    }
//
//    static public class JyRequire {
//        public @Attribute                 String name;
//        public @Attribute(name = "argvs") String value;
//
//        @NonNull
//        @Override
//        public String toString() {
//            return Mod.requireName(name) + ":" + value;
//        }
//    }
}
